using System;
using System.Data;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.ComponentModel;
using System.Collections.Generic;

public class ItemVector<TItem>
{
	protected virtual Dictionary<Guid, TItem> ItemSeries
	{
		get
		{
			string _itemTypeName = typeof(TItem).Name;
			object _obj = Environment.Context.Application[_itemTypeName];
			Dictionary<Guid, TItem> _polls;
			if (_obj is Dictionary<Guid, TItem>) {
				_polls = (Dictionary<Guid, TItem>)_obj;
			} else {
				_polls = new Dictionary<Guid, TItem>();
				_polls.Add(Guid.Empty, default(TItem));
				Environment.Context.Application[_itemTypeName] = _polls;
			}
			return _polls;
		}
	}
	
	[System.Runtime.CompilerServices.IndexerName("Serie")]
	public virtual TItem this[Guid serieId] {
		get {
			Dictionary<Guid, TItem> _series = this.ItemSeries;
			if (!_series.ContainsKey(serieId)) {
				_series.Add(serieId, default(TItem));
			}
			return _series[serieId];
		}
		set {
			Dictionary<Guid, TItem> _series = this.ItemSeries;
			if (!_series.ContainsKey(serieId)) {
				_series.Add(serieId, default(TItem));
			}
			_series[serieId] = value;
		}
	}
}

public class InitialisedItemVector<TItem> : ItemVector<TItem>
	where TItem: new()
{
	protected override Dictionary<Guid, TItem> ItemSeries {
		get {
			Dictionary<Guid, TItem> _result = base.ItemSeries;
			if (null == _result[Guid.Empty]) {
				_result[Guid.Empty] = new TItem();
			}
			return _result;
		}
	}
	
	public override TItem this[Guid serieId] {
		get {
			TItem _item = base[serieId];
			if (null == _item) {
				base[serieId] = new TItem();
				_item = base[serieId];
			}
			this.MaintainCollection(_item);
			return _item;
		}
		set {
			this.MaintainCollection(base[serieId]);
			base[serieId] = value;
		}
	}
	
	protected virtual void MaintainCollection(TItem serie)
	{
	}
}

[DataObject(true)]
public abstract class BasicService<TItemCollection> : InitialisedItemVector<TItemCollection>
	where TItemCollection: System.Collections.ICollection, new()
{
}